import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: MENG
 * Date: 2022-07-09
 * Time: 20:49
 */
public class StackTestDemo {
    //出栈入栈次序匹配
    public boolean isPopOrder(int[] pushA, int[] popA){
        if(pushA.length ==0 || popA.length == 0){
            return false;
        }
        Stack<Integer> stack = new Stack<>();
        int j = 0;
        for (int i = 0; i < pushA.length; i++) {
            stack.push(pushA[i]);
//            while(j < popA.length && !stack.empty() && stack.peek() == popA[j])
            while(j < popA.length && !stack.empty() && stack.peek().equals(popA[j])){
                stack.pop();
                j++;
            }

        }
        return stack.empty();
    }

    //逆波兰表达式求值
    public int evalRPN(String[] tokens){
        Stack<Integer> stack = new Stack<>();
        for (String x:tokens) {
            if(!isOperation(x)){
                //stack.push(Integer.valueOf(x))
                stack.push(Integer.parseInt(x));//查一下什么意思
            }else{
                //先pop出来的作为运算符的右操作数，后出来的作为左操作数
                int num2 = stack.pop();
                int num1 = stack.pop();
                switch(x){
                    case "+":
                        stack.push(num1+num2);
                        break;
                    case "-":
                        stack.push(num1-num2);
                        break;
                    case "*" :
                        stack.push(num1*num2);
                        break;
                    case "/" :
                        stack.push(num1/num2);
                        break;
                }
            }

        }
        return stack.pop();

    }
    //判断字符是否是加减乘除
    private boolean isOperation(String opera){
        if(opera.equals("+") || opera.equals("-") || opera.equals("*") || opera.equals("/")){
            return true;
        }
        return false;
    }


    //括号匹配问题
    public boolean isValid(String s){
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            //如果是左括号
            if(ch == '(' || ch == '[' || ch == '{'){
                stack.push(ch);
            }else{
                //走到这里ch就是右括号了
                //全是右括号，没有左括号
                if(stack.empty()){
                    return false;
                }
                char top = stack.peek();
                if(ch == ')' && top == '(' || ch == ']' && top == '[' || ch == '}' && top == '{'){
                    //只能说明 当前的字符是匹配的
                    stack.pop();
                }else{
                    return false;
                }
            }
        }
        //字符串已经遍历完成了，要看一下栈是为空
        //走到这里，证明括号都匹配了，但是如果stack不是空的，那么左括号就多了。
        if(stack.empty()){
            return true;
        }else{
            return false;
        }
    }
    //模拟实现栈
    public static void main2(String[] args) {
        MyStack myStack = new MyStack();
        myStack.push(1);
        myStack.push(2);
        myStack.push(3);
        myStack.push(4);
        myStack.push(5);
        System.out.println(myStack.peek());
        myStack.pop();
        System.out.println(myStack.peek());
        System.out.println(myStack.getUsedSize());



    }

    public static void main(String[] args) {
        //Stack的相关方法
        Stack<Integer> stack = new Stack<Integer>();
        stack.push(1);
        stack.push(2);
        stack.push(3);
        System.out.println(stack);
        stack.pop();
        System.out.println(stack);
        System.out.println(stack.peek());

        System.out.println(stack.size());
        boolean flg = stack.empty();
        System.out.println(flg);

        System.out.println("================");
        stack.pop();
        stack.pop();
        flg = stack.empty();
        System.out.println(flg);
    }
}